package com.xjscrm.console.chat.dispatch.match.impl;

import com.xjscrm.common.enums.ChatDispatchConditionModeEnum;
import com.xjscrm.console.chat.dispatch.match.DispatchCondition;
import com.xjscrm.console.dto.chat.setting.ChatDispatchConditionJsonDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/20 10:07
 */
@Component
public class KeywordsDispatchCondition implements DispatchCondition {

    @Override
    public Boolean match(String talkContent, ChatDispatchConditionJsonDTO dispatchCondition) {

        if (ChatDispatchConditionModeEnum._100.getCode().equals(dispatchCondition.getMode())) {
            return true;
        }
        if (Objects.equals(dispatchCondition.getOpenStrict(), 1)) {
            List<String> strictKeyWords = dispatchCondition.getStrictKeyWords();
            return strictKeyWords.stream().anyMatch(item -> StringUtils.equals(item, talkContent));
        }
        if (Objects.equals(dispatchCondition.getOpenBlur(), 1)) {
            List<String> blurKeyWords = dispatchCondition.getBlurKeyWords();
            return blurKeyWords.stream().anyMatch(item -> StringUtils.containsAny(talkContent, item));
        }
        return true;
    }
}
